﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Text.Json;

namespace MyWebApi.Infrostructure;

public static class JsonExtensions
{
    public static string ToJson<T>(this T obj, bool indented = false)
    {
        var jsonSerializerOptions = new JsonSerializerOptions
        {
            WriteIndented = indented,
            Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        };

        var json = JsonSerializer.Serialize<T>(obj, jsonSerializerOptions);
        return json;
    }
}
